/*
 * InputManager.cpp
 *
 *  Created on: 23-07-2013
 *      Author: Jarek
 */

#include "InputManager.h"
#include "ECameraLogic.h"

InputManager::InputManager(CameraLogic* camera) :
	isTouch				(false),
	camera				(camera),
	distance			(80.0f),
	horizontalAngle		(DEG2RAD(-165.0f)),
	verticalAngle		(DEG2RAD(30.0f)),
	at					(Vector3<float>(0.0f, 0.0f, 0.0f)),
	touchStart			(Vector2<int>(0, 0)),
	rotationSensivity	(0.005f),
	movementSensivity	(0.1f)
{
	camera->setSpherical(at, horizontalAngle, verticalAngle, distance);
}

InputManager::~InputManager()
{
	// TODO Auto-generated destructor stub
}

void InputManager::onRun()
{
	camera->setSpherical(at, horizontalAngle, verticalAngle, distance);
}

void InputManager::handleInput(const ETime& time, const Input& input)
{
	switch (input.inputType)
	{
		case Input::INPUT_TYPE_TOUCH:
			isTouch	= true;
			touchStart.x = input.x;
			touchStart.y = input.y;
			break;

		case Input::INPUT_TYPE_MOVE:
			if(touchStart.x <= deviceInfo.getScreenWidth() / 2)
			{
				at.x += static_cast<float>(input.y * sin(-horizontalAngle) + input.x * cos(-horizontalAngle) * movementSensivity);
				at.z += static_cast<float>(input.x * sin(-horizontalAngle) + input.y * -cos(horizontalAngle) * movementSensivity);

				LOGE("HANDLE INPUT INPUT MOVE: %d %d", input.x, input.y, at.x, at.y);
			}
			else
			{
				horizontalAngle -= static_cast<float>(input.x) * rotationSensivity;
				verticalAngle	+= static_cast<float>(input.y) * rotationSensivity;

				if(verticalAngle < DEG2RAD(5.0f))
					verticalAngle = DEG2RAD(5.0f);
				else if(verticalAngle > DEG2RAD(89.0f))
					verticalAngle = DEG2RAD(89.0f);
			}
			break;

		case Input::INPUT_TYPE_RELEASE:
			isTouch = false;
			break;
	}
}
